package org.wu.framework.inner.lazy.orm.spring.config;

import org.wu.framework.inner.lazy.orm.spring.prop.LazyDataSourceProperties;
import lombok.Data;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;

/**
 * description 懒人数据源配置 (可选)
 * 默认使用 MysqlDataSource
 *
 * @author Jia wei Wu
 * @date 2021/4/23 下午1:39
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Data
@Configuration
@ConditionalOnProperty(prefix = "spring.datasource", value = "url")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class LazyDataSourceAutoConfiguration {


    /**
     * description 添加懒人数据源配置
     *
     * @param
     * @param lazyDataSourceProperties 数据源连接配置
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2021/4/23 下午1:54
     */
    @Bean(name = "lazyDataSource")
    @ConditionalOnMissingBean(DataSource.class)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public DataSource lazyDataSource(LazyDataSourceProperties lazyDataSourceProperties) {
        return lazyDataSourceProperties.initializeDataSourceBuilder().build();
    }


}
